package com.yuexunit.finance.system.service;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.system.entity.CostInfoEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @author wenwenlong
 */
@Implements(CostInfoService.class)
public class CostInfoServiceImpl implements CostInfoService {
	
	@Override
	@Transactional
	public Long createCostInfo(CostInfoEntity entity) {
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		
		PersistUtil.insert(entity);
		return entity.getCostInfoId();
	}

	@Override
	@Transactional
	public void disableCostInfo(Long costInfoId) {
		CostInfoEntity entity = PersistUtil.get(CostInfoEntity.class, costInfoId);
		entity.setEnabled(Boolean.FALSE);
		entity.setUpdateDateTime(DateUtil.now());
		
		PersistUtil.update(entity);
	}

	@Override
	public PageList<CostInfoEntity> ipuireCostInfoList(String costInfoNumber, String cosntInfoType, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CostInfoEntity.class);
		if (StringUtil.isNotBlank(costInfoNumber)) {
			criteria.add(Restrictions.like("costInfoNumber", "%" + costInfoNumber + "%"));
		}
		
		if (StringUtil.isNotBlank(cosntInfoType)) {
			criteria.add(Restrictions.like("cosntInfoType", "%" + cosntInfoType + "%"));
		}
		
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		
		PageList<CostInfoEntity> list = PersistUtil.get(criteria, page);
		
		return list;
	}
}
